<template>
  <div class="app-container">
        <el-row :gutter="20">
      <el-col :span="6">
        <el-input
          placeholder="请输入报告名称"
          prefix-icon="el-icon-search"
          v-model="search.report_name"
          @keyup.enter.native="SearchReport()">
        </el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" icon="el-icon-search" round @click="SearchReport()">搜索</el-button>
      </el-col>
    </el-row>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" min-width="5%">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="报告名称" min-width="25%" show-overflow-tooltip>
        <template slot-scope="scope">
        <el-link @click="get_report(scope.row.id)" style="color:#298abd">{{ scope.row.report_name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="运行结果" min-width="6%" align="center">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status == true ? 'success': 'danger'">
            {{ scope.row.status == true ? '成功' : '失败'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="总计用例" min-width="6%">
        <template slot-scope="scope">
          {{ scope.row.testsRun }}
        </template>
      </el-table-column>
      <el-table-column label="成功用例" min-width="6%">
        <template slot-scope="scope">
          {{ scope.row.successes }}
        </template>
      </el-table-column>

      <el-table-column align="center" prop="created_at" label="运行开始时间" min-width="12%">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.start_at }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运行者" min-width="10%" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="10%" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-tooltip content="下载" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-download"
                @click="download(scope.row.id, scope.row.report_name)">
              </el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row.id)">
              </el-button>
            </el-tooltip>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.page_num"
      :page-sizes="[10, 20]"
      :page-size="pagination.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total">
    </el-pagination>
  </div>
</template>

<script>
import { getReportList,  getReport, deleteReport, downloadReport } from '@/api/test_report/table'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: {
        report_name: ''
      },
      pagination: {
        total: 0,
        page_current: 1,
        page_num: 1,
        page_size: 10
      },
      list: null,
      listLoading: true,
      ispagination: false
    }
  },
  created() {
    this.fetchReportData()
  },

  methods: {
    fetchReportData() {
      this.listLoading = true
      var params = { 'ordering': '-update_time' }
      getReportList(params).then(response => {
        this.list = response.results
        this.listLoading = false
        this.pagination.total = response.count
      })
    },

    get_report(params){
      getReport(params).then( response=> {
        console.log(response)
        window.localStorage.removeItem('callbackHTML');
        window.localStorage.setItem('callbackHTML', response)
        var newWin = window.open('', '_blank')
        newWin.document.write(localStorage.getItem('callbackHTML'))
        newWin.document.close()
      })
    },

    SearchReport: function() {
      var params = {}
      if (this.search.report_name) {
        params['search'] = this.search.report_name
      }

      if (this.ispagination){
        params['page_num'] = this.pagination.page_num
        params['page_size'] = this.pagination.page_size
      }else{
        params['page_num'] = 1
        params['page_size'] = this.pagination.page_size
      }
      params['ordering'] = '-update_time'

      this.listLoading = true
      getReportList(params).then(response => {
        this.list = response.results
        this.listLoading = false
        this.pagination.total = response.count
      })
      if (this.ispagination == false){
        this.pagination.page_num = 1
       }
        this.ispagination = false
     },

    download(id, name) {
      var params = {}
      params['id'] = id
      downloadReport(params).then(response => {
        console.log(response.size )
        if (response.size === 76) {
             this.$message.error('指定的报告不存在，请确认后再操作')
          } else {
              const blob = new Blob([response])
              let fileName = name + '.html'
              const link = document.createElement('a')
              link.download = fileName
              link.style.display = 'none'
              link.href = URL.createObjectURL(blob)
              document.body.appendChild(link)
              link.click()
              URL.revokeObjectURL(link.href)
              document.body.removeChild(link)
        }
      })
    },

    del(arg){
      this.$confirm('此操作将删除报告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
      var params = {}
      params['id'] = arg
      deleteReport(params).then(response => {
        if (response.status == "000000") return
          this.SearchReport()
      })
        this.$message({
          type: 'success',
          message: '删除成功!',
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    handleSizeChange: function(val) {
      this.pagination.page_size = val
      this.SearchReport()
    },
    handleCurrentChange: function(val) {
      this.ispagination = true
      this.pagination.page_num = val
      this.SearchReport()
    }
  }
}
</script>
